﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using AvalonDock;
using Microsoft.Practices.ServiceLocation;
using System.IO;

namespace DynamicUIFramework
{
    /// <summary>
    /// Shell.xaml 的交互逻辑
    /// </summary>
    [Export]
    public partial class Shell : Window
    {
        public Shell()
        {
            InitializeComponent();

            this.dm.DeserializationCallback += (s, e) =>
            {
                var dockableContent = new RegionContent(e.Name);
                e.Content = dockableContent;
            };

            RegionController rc = ServiceLocator.Current.GetInstance<RegionController>();
            foreach (var item in rc.Regions)
            {
                MenuItem mi = new MenuItem { Header = item.Key, Tag = item.Key };
                mi.Click += (s, ee) =>
                {
                    RegionContent r = new RegionContent((s as MenuItem).Tag.ToString());
                    r.Load();
                    this.dm.MainDocumentPane.Items.Add(r);
                };
                this.miAddExist.Items.Add(mi);
            }

            bool isin=  DesignerContext.IsInDesignMode;
            MenuItem miM = new MenuItem { Header = isin == true ? "OutDesigner" : "InDesigner", Tag = isin };
            miM.Click += (s, e) => {
                bool isIn = bool.Parse((s as MenuItem).Tag.ToString());
                DesignerContext.IsInDesignMode = !isIn;
            };
            this.miMode.Items.Add(miM);
        }

        public void RestoreLayout()
        {
            if (File.Exists(UISetting.LayoutPath))
                this.dm.RestoreLayout(UISetting.LayoutPath);
        }

        private void miAdd_Click(object sender, RoutedEventArgs e)
        {
            NameDialog d = new NameDialog();
            bool? ret = d.ShowDialog();
            if (ret == true)
            {
                if (string.IsNullOrEmpty(d.TargetName) || string.IsNullOrEmpty(d.TargetTitle))
                {
                    MessageBox.Show("名字和标题不能为空！");
                    return;
                }

                foreach (var item in this.dm.DockableContents)
                {
                    if (item.Name.Equals(d.TargetName))
                    {
                        MessageBox.Show("已经存在相同名字对象");
                        return;
                    }
                }
                RegionController rc =ServiceLocator.Current.GetInstance<RegionController>();
                if (rc.Regions.ContainsKey(d.TargetName))
                {
                    MessageBox.Show("在本地目录中已经存在相同名字对象");
                    return;
                }
                RegionContent r = new RegionContent(d.TargetName);
                r.Title = d.TargetTitle;
                r.Content = new DesignerCanvas();
                r.Background = Brushes.White;
                (r.Content as DesignerCanvas).Background = r.Background;
                this.dm.MainDocumentPane.Items.Add(r);
            }
        }

        private void miSave_Click(object sender, RoutedEventArgs e)
        {
            this.dm.SaveLayout(UISetting.LayoutPath);
        }

        private void miLoad_Click(object sender, RoutedEventArgs e)
        {
            RestoreLayout();
        }


        private void SetDefaultTheme(object sender, RoutedEventArgs e)
        {
            ThemeFactory.ResetTheme();
        }

        private void ChangeCustomTheme(object sender, RoutedEventArgs e)
        {
            string uri = (string)((MenuItem)sender).Tag;
            ThemeFactory.ChangeTheme(new Uri(uri, UriKind.RelativeOrAbsolute));
        }

        private void ChangeStandardTheme(object sender, RoutedEventArgs e)
        {
            string name = (string)((MenuItem)sender).Tag;
            ThemeFactory.ChangeTheme(name);
        }

        private void ChangeColor(object sender, RoutedEventArgs e)
        {

            ThemeFactory.ChangeColors((Color)ColorConverter.ConvertFromString(((MenuItem)sender).Header.ToString()));
        }

    }
}
